package com.fengxing.ams.pub.util;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class SimpleDateFormatThreadLocal {
    private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    private static ThreadLocal<Map<String, SimpleDateFormat>> simpleDateFormatThreadLocal =
            new ThreadLocal<Map<String, SimpleDateFormat>>() {
        protected synchronized Map<String, SimpleDateFormat> initialValue() {
            return new HashMap<String, SimpleDateFormat>();
        }
    };
    
    public static SimpleDateFormat get() {
        return get(DEFAULT_FORMAT);
    }
    
    public static SimpleDateFormat get(String dateFormat) {
        Map<String, SimpleDateFormat> dateFormatMap = simpleDateFormatThreadLocal.get();
        SimpleDateFormat sdf = dateFormatMap.get(dateFormat);
        if (sdf == null) {
            sdf = new SimpleDateFormat(dateFormat);
            dateFormatMap.put(dateFormat, sdf);
        }
        return sdf;
    }
    
    public static void clear() {
        simpleDateFormatThreadLocal.remove();
    }
}
